<template>
  <div>
    <div v-if="enterpriseInfo">
      <top-info :enterprise-info="enterpriseInfo" :buy-info="buyInfo" :is-expire="isExpire" @levelUp="levelUp"
                @toEditOrg="toEditOrg"></top-info>
      <div class="midBox"></div>
      <org-right :level="buyInfo.subscriptionId" :is-expire="isExpire" @levelUp="levelUp" @exitOrg="exitOrg"></org-right>
    </div>
    <div v-else>
      <no-join-enterprise></no-join-enterprise>
    </div>
  </div>
</template>

<script>
  import TopInfo from "./components/TopInfo";
  import OrgRight from "./components/OrgRight";
  import {Dialog, Toast} from 'vant'
  import TeamAPI from "../../../repo/api/TeamAPI";
  import noJoinEnterprise from "../../../components/noJoinEnterprise";
  import EnterpriseLevelAPI from "../../../repo/api/EnterpriseLevelAPI";
  import store from "../../../repo/store/store";
  import {userActionsTypes} from "../../../repo/store/user";
  import {authActionsTypes} from "../../../repo/store/auth";

  export default {
    name: "index",
    components: {
      TopInfo,
      OrgRight,
      noJoinEnterprise
    },
    data() {
      return {
        enterpriseInfo: {},
        enterpriseUserInfo: {},
        level: [45, 46, 47, 48],
        buyInfo: {},
        isExpire: false,
      }
    },
    computed: {
      imCreator() {
        if (store.getters.enterpriseRoleInfo) {
          let roleType = store.getters.enterpriseRoleInfo.roleType
          return roleType === 1 || roleType === 2
        } else {
          return false
        }
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        Toast.loading({
          duration: 0,
          message: '正在加载',
          forbidClick: true,
          loadingType: 'spinner',
        })
        EnterpriseLevelAPI.getEnterpriseInfo().then((res) => {
          if (res.responseData) {
            this.enterpriseInfo = res.responseData.enterprise
          } else {
            this.enterpriseInfo = {}
          }
        }).catch(error => {
          console.log(error)
        })
        EnterpriseLevelAPI.getGroupLevelInfo().then((res) => {
          console.log(res);
          if (res.responseData.buyInfo) {
            //有套餐
            this.buyInfo = res.responseData.buyInfo;
          } else if(res.responseData.lastLevelInfo) {
            //套餐已过期
            this.isExpire = true;
            this.buyInfo = res.responseData.lastLevelInfo;
          }else {
            //未开通过套餐
            this.buyInfo = {
              subscriptionId: 0
            }
          }
        });
        Toast.clear()
      },
      toEditOrg() {
        if (!this.imCreator) {
          Toast('只有企业管理员才能编辑企业信息')
        } else {
          this.$_router.push(`/editOrg`)
        }

      },
      exitOrg() {
        if (this.imCreator) {
          if (this.buyInfo.subscriptionId === 45 || this.buyInfo.subscriptionId === 0) {
            //试用版企业管理员退出企业
            Dialog.confirm({
              title: '是否退出企业',
              message: '您是管理员，退出后企业将解散',
              confirmButtonText: '确定退出',
            }).then(() => {
              TeamAPI.disbandFreeEnterprise().then((result) => {
                Toast('退出成功')
                let hsId = store.getters.hsId;
                let loginType = store.getters.loginType;
                store.dispatch(authActionsTypes.CLEAN_LOGGED_INFO, {}).then(_ => {
                  setTimeout(() => {
                    window.location.href = window.location.origin + `/web-hft-ee/notInvolved/${loginType}/` + hsId;
                  }, 500)
                });
              }).catch(error => {
                console.log(error)
              })
            }).catch(() => {
              console.log('选择取消')
            })
          } else {
            Toast('管理员无法退出企业')
          }


        } else {
          //企业成员退出企业
          Dialog.confirm({
            title: '退出企业',
            message: '确定要退出企业吗',
          }).then(() => {
            TeamAPI.quitTeam().then((result) => {
              Toast('退出成功')
              let hsId = store.getters.hsId;
              let loginType = store.getters.loginType;
              store.dispatch(authActionsTypes.CLEAN_LOGGED_INFO, {})
                .then(_ => {
                  setTimeout(() => {
                    window.location.href = window.location.origin + `/web-hft-ee/notInvolved/${loginType}/` + hsId;
                  }, 500)
                });
            }).catch(error => {
              console.log(error)
            })
          }).catch(() => {
            console.log('选择取消')
          })
        }

      },
      levelUp(param) {
        let hsId = store.getters.hsId
        let loginType = store.getters.loginType;
        if (param === 'intro') {
          //跳介绍页
          this.$router.push({path: `/package-list/${loginType}/${hsId}/#intro`})
        } else {
          this.$router.push({path: `/package-list/${loginType}/${hsId}`})
        }
      },
    }
  }
</script>

<style scoped>
  .midBox {
    height: 120px;
    background: white;

  }
</style>
